#include <iostream>
#include <fcntl.h>
#include <unistd.h>

void SetNoBlock()
{
    int n = ::fcntl(0, F_GETFL);
    if(n < 0)
        std::cout << "Error" << std::endl;
    else   
        fcntl(0, F_SETFL, n | O_NONBLOCK);
}


int main() 
{
    SetNoBlock();
    while(true)
    {
        char buffer[1024];
        ssize_t n = ::read(0, buffer, sizeof(buffer));
        if(n > 0)
        {
            std::string str = buffer;
            std::cout << str << std::endl;
        }
        else if(n == 0)
        {
            std::cout << "read done!" << std::endl;
            break;
        }
        else
        {
            // errno == 11 EWOULDBLOCK EAGAIN
            if(errno == EWOULDBLOCK)
            {
                continue;
            }
            std::cout << "GG" << std::endl;
            sleep(1);
        }
    }
    return 0;
}